<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/layui/css/layui.css" media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>

<table class="layui-hide" id="test" lay-filter="test"></table>
<span id="show" style="display:none">123</span>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <!--    <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>-->
        <!--    <button class="layui-btn layui-btn-sm" lay-event="isAll" id="all">验证是否全选</button>-->
    </div>
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
</script>


<script src="/static/layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

<script>
    layui.use(['table', 'laypage', 'layer','jquery', 'form', 'laydate'], function () {
        var table = layui.table;
        var $ = layui.$;
        var form = layui.form;
        var layer = layui.layer;

        table.render({

            elem: '#test',
            url: '/team/getAllTeam',
            toolbar: '#toolbarDemo',
            title: '团队数据表',
            totalRow: true,
            cols: [
                [{
                    type: 'checkbox',
                    fixed: 'left'
                }, {
                    field: 'id',
                    title: 'ID',
                    width: 180,
                    // fixed: 'left',
                    unresize: true,
                    sort: true,

                }, {
                    field: 'name',
                    title: '团队名称',
                    width: 300,
                    edit: 'text'
                }, {
                    field: 'advisor',
                    title: '导师',
                    width: 100,
                    sort: true,
                    // totalRow: true
                }, {
                    field: 'honor',
                    title: '荣誉',
                    width: 650,
                    edit: 'text',
                    sort: true
                }, {
                    field: 'state',
                    title: '团队状态',
                    width: 150,
                    edit: 'text'
                }, {
                    fixed: 'right',
                    width: 178,
                    align: 'center',
                    toolbar: '#barDemo'
                }]
            ],
            page: false
        });

        var arr = [];
        table.on('checkbox(test)', function (obj) {
            var flg = 0;
            if (obj.type != 'all') {
                if (arr.length == 0) {
                    if (obj.checked == true) {
                        arr.push(obj);

                    }
                } else {
                    $.each(arr, function (index, value) {
                        if (obj.data.id == value.data.id) {
                            flg = 1;
                            if (obj.checked == false) {
                                arr.splice(index, 1);
                            }
                        } else {
                            flg = 0;
                        }

                    });
                    if (flg == 0) {
                        arr.push(obj);
                    }
                }
            }
        });


        //工具栏事件
        table.on('toolbar(test)', function(obj) {
            var checkStatus = table.checkStatus(obj.config.id);

            switch (obj.event) {
                case 'getCheckData':
                    var data = checkStatus.data;
                    $(this).parent().parent().parent().parent().toggle();
                    $("#show").toggle();
                    $("#all").toggle();
                    console.log(data)
                    $('#show').html(JSON.stringify(data))
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    console.log(checkStatus)
                    layer.msg('选中了：' + data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选' : '未全选')
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                console.log(obj.data);
                layer.open({
                    //layer提供了5种层类型。可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）
                    type: 1,
                    title: "修改团队信息",
                    area: ['700px', '500px'],
                    content: $("#popUpdateTest")//引用的弹出层的页面层的方式加载修改界面表单
                });
                //表单初始赋值
                setForm(data);
                //动态向表传递赋值可以参看文章进行修改界面的更新前数据的显示，当然也是异步请求的要数据的修改数据的获取
                setFormValue(obj, data);
            }
        });

        //监听弹出框表单提交，massage是修改界面的表单数据,submit(sub)是修改按钮的绑定
        function setFormValue(obj, data) {
            form.on('submit(sub)', function (massage) {
                console.log(massage);
                $.ajax({
                    url: '/team/updateTeamInfo',
                    type: 'POST',
                    data: {
                        id: data.id,  //原来的数据：id
                        name: massage.field.name,
                        advisor: massage.field.advisor,
                        state: massage.field.state,
                    },
                    success: function (res) {
                        var returnCode = res.code;//取得返回数据（Sting类型的字符串）的信息进行取值判断
                        console.log(res.code);
                        console.log(res);
                        if (returnCode == 0) {
                            layer.closeAll('loading');
                            layer.load(2);
                            layer.msg("修改成功", {icon: 6});
                            setTimeout(function () {
                                obj.update({
                                    name: massage.field.name,
                                    advisor: massage.field.advisor,
                                    state: massage.field.state,
                                });//修改成功修改表格数据不进行跳转
                                layer.closeAll();//关闭所有的弹出层
                            }, 1000);
                            //加载层-风格
                        } else {
                            layer.msg("修改失败", {icon: 6});
                        }
                    }
                })
                return false;
            })

        }

        //表单初始赋值
        function setForm(data) {
            form.val('team', {
                "name": data.name   // 格式："name": "value"
                , "advisor": data.advisor
                , "state": data.state
            })
        }
    });
</script>

<div class="layui-row" id="popUpdateTest" style="display:none;">
    <div class="layui-col-md10">
        <form   class="layui-form layui-from-pane"  lay-filter="team" action="" style="margin-top:20px" >

            <div class="layui-form-item">
                <label class="layui-form-label">团队名字 </label>
                <div class="layui-input-block">
                    <input type="text" name="name"   lay-verify="required" autocomplete="off" placeholder="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">导师</label>
                <div class="layui-input-block">
                    <input type="text" name="advisor" lay-verify="required"  autocomplete="off" placeholder="" class="layui-input">
                </div>
            </div>


            <div class="layui-form-item">
                <label class="layui-form-label">团队状态</label>
                <div class="layui-input-block">
                    <select name="state" lay-verify="required">
                        <option value=""></option>
                        <option value="1">已结题</option>
                        <option value="2">未结题</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item" style="margin-top:40px">
                <div class="layui-input-block">
                    <button class="layui-btn  layui-btn-submit " lay-submit="" lay-filter="sub">确认修改</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>

        </form>
    </div>
</div>
</body>
</html>
